package com.tledu.email.service.impl;

import java.util.List;

import com.tledu.common.utils.DateUtils;
import com.tledu.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tledu.email.mapper.EmailBinMapper;
import com.tledu.email.domain.EmailBin;
import com.tledu.email.service.IEmailBinService;

/**
 * 垃圾箱Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-02
 */
@Service
public class EmailBinServiceImpl implements IEmailBinService {
    @Autowired
    private EmailBinMapper emailBinMapper;

    /**
     * 查询垃圾箱
     *
     * @param id 垃圾箱主键
     * @return 垃圾箱
     */
    @Override
    public EmailBin selectEmailBinById(Long id) {
        return emailBinMapper.selectEmailBinById(id);
    }

    /**
     * 查询垃圾箱列表
     *
     * @param emailBin 垃圾箱
     * @return 垃圾箱
     */
    @Override
    public List<EmailBin> selectEmailBinList(EmailBin emailBin) {
        return emailBinMapper.selectEmailBinList(emailBin);
    }

    /**
     * 新增垃圾箱
     *
     * @param emailBin 垃圾箱
     * @return 结果
     */
    @Override
    public int insertEmailBin(EmailBin emailBin) {
        emailBin.setCreateTime(DateUtils.getNowDate());
        return emailBinMapper.insertEmailBin(emailBin);
    }

    /**
     * 修改垃圾箱
     *
     * @param emailBin 垃圾箱
     * @return 结果
     */
    @Override
    public int updateEmailBin(EmailBin emailBin) {
        emailBin.setUpdateTime(DateUtils.getNowDate());
        return emailBinMapper.updateEmailBin(emailBin);
    }

    /**
     * 批量删除垃圾箱
     *
     * @param ids 需要删除的垃圾箱主键
     * @return 结果
     */
    @Override
    public int deleteEmailBinByIds(Long[] ids) {
        return emailBinMapper.deleteEmailBinByIds(ids);
    }

    /**
     * 删除垃圾箱信息
     *
     * @param id 垃圾箱主键
     * @return 结果
     */
    @Override
    public int deleteEmailBinById(Long id) {
        return emailBinMapper.deleteEmailBinById(id);
    }

    // 查找当前用户的垃圾箱邮件列表
    @Override
    public List<EmailBin> selectBinList(EmailBin emailBin) {
        // 可以传入到垃圾箱的数据肯定是收件人发件人有一方是当前用户
        Long userId = SecurityUtils.getUserId();
        Long recipientsId = emailBin.getRecipientsId();
        // 当前用户=收件人
        if (recipientsId == userId) {
            emailBin.setRecipientsId(userId);
            return emailBinMapper.selectRecipientsIdBinList(emailBin);
        } else {  // 当前用户=发件人
            emailBin.setCreateId(userId);
            return emailBinMapper.selectCreateIdBinList(emailBin);
        }
    }

    // 根据用户查找当前用户的的垃圾箱邮件列表
    @Override
    public List<EmailBin> selectBinByCondition(EmailBin emailBin) {
        // 可以传入到垃圾箱的数据肯定是收件人发件人有一方是当前用户
        Long userId = SecurityUtils.getUserId();
        Long recipientsId = emailBin.getRecipientsId();
        // 当前用户=收件人
        if (recipientsId == userId) {
            emailBin.setRecipientsId(userId);
            return emailBinMapper.selectRecipientsIdConditionBinList(emailBin);
        } else {  // 当前用户=发件人
            emailBin.setCreateId(userId);
            return emailBinMapper.selectCreateIdConditionBinList(emailBin);
        }
    }

    // 逻辑删除，实际上的更新操作
    @Override
    public int updateSymbol(Long[] ids) {
        return emailBinMapper.updateSymbol(ids);
    }

    // 还原操作，实际上是更新
    @Override
    public int updateByChangeSymbol(Long[] ids) {
        return emailBinMapper.updateByChangeSymbol(ids);
    }
}
